﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using JGIC.CommandLine.Utility;

using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace rtf2txt
{
    class Program
    {

        static void Main(string[] args)
        {
            //mgmt CLi args
            Arguments CommandLine = new Arguments(args);

            #region debug - verbose
            //debug - verbose (not really complete and usefull as a user)
            bool d = false;

            if (CommandLine["verbose"] != null | CommandLine["v"] != null)
            {
                d = true; //debug
            }
            #endregion

            #region StdIO
            //Stadard output or not (default not)
            bool stdout = false;
            #endregion

            #region set variables from args

            #region help...

            bool help = false; // for the help CLI
            if (CommandLine["help"] != null | CommandLine["h"] != null | args[0] == "/?" | args[0] == "/help" | args[0] == "/h") help = true;
            #endregion

            String fileOut = "";
            String fileIn = "";
            bool Overwriting = false;

            //we passes through argument and if they are used, we fills our vars
            if (CommandLine["stdout"] != null) stdout = true;
            if (CommandLine["s"] != null) stdout = true;
            if (CommandLine["fileout"] != null) fileOut = CommandLine["fileout"];
            if (CommandLine["o"] != null) fileOut = CommandLine["o"];
            if (CommandLine["file"] != null) fileIn = CommandLine["file"];
            if (CommandLine["f"] != null) fileIn = CommandLine["f"];
            if (CommandLine["overwrite"] != null) Overwriting = true;
            if (CommandLine["w"] != null) Overwriting = true;
            #endregion



            if (fileIn == "" | (args.Length < 1) | help == true)
            {
                Console.WriteLine("\nRtf2Text Jgi.Tools.CLi Utilities\nVersion 1.0.0.1.2009.07.25\nby Jean-Guillaume Isabelle (2009)\n\nUsage:\n\n"
                    + "--file, -f [filename]\n"
                    + "--stdout, -s (output to standard) (Default output)\n"
                    + "--xml, -x (output in XML the formatting) (NOT IMPLEMENTED YET)\n"
                    + "--fileout, -o [filename]\n"
                    + "--verbose, -v (verbose info printed)\n"
                    + "--overwrite, -w (Overwrite existing file (default won't))\n"
                    );
            }
            #region else (Basically every implements (If we do not print the help, we do someting ;)

            else
            {
                if (d) Console.WriteLine("file: " + fileIn + "\n");

                //we use this to do the job
                Jgi.Tools.JgiAsciiConverter cv = new Jgi.Tools.JgiAsciiConverter();

                //content
                String c;

                //2 StdIO
                if (stdout | fileOut == "")
                {
                    c = cv.LoadFileContent(fileIn);
                    if (d) Console.WriteLine("c: " + c + "\n");

                    //to Console (StdIO)
                    Console.WriteLine(cv.ConverterRtf2Text(c));
                }

                //2 The file output
                if (fileOut != "")
                {
//                    if (File.Exists(fileOut) & Overwriting == true)
                    if (Overwriting == true)
                    {
                        cv.LoadAndSerializeContent(fileIn, fileOut,true);
                    }
                    else
                    {
                        cv.LoadAndSerializeContent(fileIn, fileOut, false);
                        //TODO BUG: There are buggies and work to do here, find a way to use or not this extension
                        //                        Console.WriteLine("\nError\nFile exist\n\nUse: --overwrite, -w\n\nquitting...");
                    }
                }

            }
            #endregion else implements ends
        }
    }
}
    namespace Jgi.Tools
    {
        public class JgiAsciiConverter
        {

            /// <summary>
            /// Take Rtf as input and return Text
            /// </summary>
            /// <param name="Rtf"></param>
            /// <returns>Rtf content</returns>
            public String ConverterRtf2Text(String Rtf)
            {
                System.Windows.Forms.RichTextBox richtextbox = new System.Windows.Forms.RichTextBox();
                richtextbox.Rtf = Rtf;
                return richtextbox.Text;
            }


            public String LoadFileContent(String path)
            {
                if (File.Exists(path))
                {
                    //String content = File.ReadAllText(path);
                    //return content;
                    return File.ReadAllText(path);
                }
                else
                {
                    Console.WriteLine("Failed loading");
                    return "Failed";
                }
            }

            public void LoadAndSerializeContent(String fileIn, String fileOut)
            {
                String c = ConverterRtf2Text(LoadFileContent(fileIn));
                SerializeFile(fileOut, c);
            }


            //humm...useless
            public void LoadAndSerializeContent(String fileIn, String fileOut, bool Overwritepath)
            {
                //String c = LoadFileContent(fileIn);
                //SerializeFile(fileOut, c, Overwritepath);
                SerializeFile(fileOut, LoadFileContent(fileIn), Overwritepath);
            }


            public void SerializeFile(String path, String content)
            {
                try
                {
                    File.WriteAllText(path, content);
                }
                catch (Exception e)
                {
                    Console.WriteLine("\nError " + e.ToString() + "\n\n\nSomething when wrong...sorry\n");
                }

            }


            //useless
            public void SerializeFile(String path, String content, bool Overwritepath)
            {
                try
                {
                    if (File.Exists(path))
                    {
                        File.WriteAllText(path, content);
                    }
                    else
                    {
                        File.WriteAllText(path, content);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("\nError " + e.ToString() + "\n\n\nSomething when wrong...sorry\n");
                    throw;
                }

            }
        }
    }

